package com.ruoyi.common.utils.qrcode;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @version :
 * @program: allmerchants
 * @description: 二维码工具生成类
 * @author: 周爱鸿
 * @create: 2020-07-22 16:08
 **/
public class QrCodeUtil {
	/**
	 * @Param: content 二维码内容
	 * @Param path
	 * @return: void
	 * @Description: 生成二维码
	 */
	public static BitMatrix orCode(String content) {
		if (null == content || "".equals(content)){
			return null;
		}
		// 图片的宽度和高度
		int width = 300;
		int height = 300;
		// 图片的格式
		String format = "png";

		//其他参数，如字符集编码
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
		// 定义字符集编码格式
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		// 纠错的等级 L > M > Q > H 纠错的能力越高可存储的越少，一般使用M
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		// 设置图片边距
		hints.put(EncodeHintType.MARGIN, 2);
		BitMatrix bitMatrix = null;
		try {
			// 最终生成 参数列表 （1.内容 2.格式 3.宽度 4.高度 5.二维码参数）
			bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
			// 生成图片
			HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
			OutputStream stream = response.getOutputStream();
			MatrixToImageWriter.writeToStream(bitMatrix, format, stream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitMatrix;
	}

	/**
	 * @Param: file
	 * @return: java.lang.String
	 * 解析二维码
	 */
	public static String decode(File file) {
		BufferedImage image;
		try {
			image = ImageIO.read(file);

			LuminanceSource source = new BufferedImageLuminanceSource(image);
			Binarizer binarizer = new HybridBinarizer(source);
			BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
			Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
			//解码设置编码方式为：UTF-8
			hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
			//优化精度
			hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
			//复杂模式，开启PURE_BARCODE模式
			hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
			//对图像进行解码
			Result result = new MultiFormatReader().decode(binaryBitmap, hints);
			return result.getText();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @Param: filePath
	 * @return: java.lang.String
	 * 解析二维码读取文件
	 */
	public static String decode(String filePath) {
		return decode(new File(filePath));
	}


	public static void main(String[] args) {
		//生成二维码
		QrCodeUtil.orCode("http://www.baidu.com");
		//解析二维码
		System.out.println(QrCodeUtil.decode("F:/pfk/allmerchants/code.jpg"));
	}
}
